#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @author           = 'zhouyi'
# @mail             = '6098550@qq.com'
# @lastModifiedTime = 2017-02-28


#from ctypes import *
import ctypes
import binascii



BLOCK0_EN        = 0x01
BLOCK1_EN        = 0x02
BLOCK2_EN        = 0x04
NEEDSERIAL       = 0x08
EXTERNKEY        = 0x10
NEEDHALT         = 0x20

# reture value.
ERR_REQUEST      = 8
ERR_READSERIAL   = 9
ERR_SELECTCARD   = 10
ERR_LOADKEY      = 11
ERR_AUTHKEY      = 12
ERR_READ         = 13
ERR_WRITE        = 14
ERR_NONEDLL      = 21
ERR_DRIVERORDLL  = 22
ERR_DRIVENULL    = 23
ERR_TIMEOUT      = 24
ERR_TXSIZE       = 25
ERR_TXCRC        = 26
ERR_RXSIZE       = 27
ERR_RXCRC        = 28




# RDFI wraper
class IC_Reader(object):
    # base property
    
    __api = None

    def __init__(self):
        FileName = "OUR_MIFARE.dll"
        #print FileName
        # stdcall
        self.__api = ctypes.WinDLL(FileName)
        # cdecl        
        #self.__api = ctypes.CDLL(FileName)


    # beep x ms
    def beep(self):
        self.__api.pcdbeep(50)


    # read the infomation of the ic card.
    def read(self):
        status       = ctypes.c_ubyte(0)
        area_no      = ctypes.c_ubyte(0)   # 0-15
        auth_mode    = ctypes.c_ubyte(1)   # >1 A or B password.
        ctrl_word    = ctypes.c_ubyte(BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY)
        ArrayPiccKey    = ctypes.c_ubyte * 6
        ArrayPiccSerial = ctypes.c_ubyte * 4 
        ArrayPiccData   = ctypes.c_ubyte * 48
        picc_key     = ArrayPiccKey(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
        picc_serial  = ArrayPiccSerial()
        picc_data    = ArrayPiccData()
        
        status = self.__api.piccreadex(ctrl_word, picc_serial, area_no, auth_mode, picc_key, picc_data)
        #self.__api.picchalt()
        if status == 0:
            #self.beep()
            print '-------operation success!------------'
            print "area_no=%d, auth_mode=%d, ctrl_word=%d ret=%d" % (area_no.value, auth_mode.value, ctrl_word.value, status)
        
            # card_id = ''
            # for i in range(0, 4):
            #     card_id += hex(picc_serial[i])[2:].upper()
            # return card_id
            card_data =''
            #for i in range(0, 48):        	
            for i in range(0, 12):          
                card_data += hex(picc_data[i])[2:].upper()
            return card_data


        elif status == 8:
            print 'please put the card to the address!'
        elif status == 21:
            print 'no find the dll ICUSB.dll'
        else: 
            print 'exception!'
        
        return ''


        # this serial number of the card.
        #print ctypes.string_at(picc_serial) 
        # this data of the card.
        #print ctypes.string_at(picc_data)
        #print picc_key
        #print picc_key[0]
        #print picc_serial
        # card_id = ''
        # for i in range(0, 4):
        #     card_id += hex(picc_serial[i])[2:].upper()
        # print card_id
        
        # #print picc_data
        # card_data =''
        # for i in range(0, 48):        	
        # 	card_data += hex(picc_data[i])[2:].upper()
        # print card_data
        
    # write the infomation to the ic card.
    def write(self, data):
        status       = ctypes.c_ubyte(0)
        area_no      = ctypes.c_ubyte(8)
        auth_mode    = ctypes.c_ubyte(1)   #A or B password.
        ctrl_word    = ctypes.c_ubyte(BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY)
        ArrayPiccKey    = ctypes.c_ubyte * 6
        ArrayPiccSerial = ctypes.c_ubyte * 4
        ArrayPiccData   = ctypes.c_ubyte * 48
        picc_key     = ArrayPiccKey(0xFF, 0xFF, 0xFF, 
        							0xFF, 0xFF, 0xFF)
        picc_serial  = ArrayPiccSerial()
        picc_data    = ArrayPiccData()
        # print status
        # print area_no
        # print auth_mode
        # print ctrl_word
        # print picc_key
        # print picc_key[0]
        # print picc_serial
        # print picc_serial[0]
        # print picc_data
        # print picc_data[0]
        # print self.FileName
        #ret = self.__api.piccwriteex(ctrl_word, picc_serial, area_no, auth_mode, picc_key, picc_data)
        print ret
        beep()
        #self.__api.piccreadex()



# service_url = 'http://10.0.5.33'
# iccard_id   = '1234567890'

def request_bg_service(iccard_id, service_url='http://mi.hakurei.cn/api/account/icLogin'):
    import urllib
    import urllib2

    #print iccard_id
    values = {'name':iccard_id}
    data = urllib.urlencode(values)
    print data
    request  = urllib2.Request(service_url, data)
    response = urllib2.urlopen(request)
    the_page = response.read()
    print the_page



import time
if __name__ == "__main__":
    reader = IC_Reader();
    #reader.beep()
    while 1:
        iccard_id = reader.read()
        #print iccard_id
        if iccard_id != '':
            request_bg_service(iccard_id)
        time.sleep(2)



